package com.pi.dao;

import com.pi.po.ReportPO;
import com.pi.util.MySql;

import java.sql.*;
import java.util.Map;
import java.util.Set;

public class ReceiveDao {

    public boolean insertReportData(Map<String,String[]> data) throws SQLException {
        String sql="insert into report_data(";
        Set<String> keys=data.keySet();
        String sql2=") values(";
        for(String key:keys){
            sql+=key+",";
            sql2+="'"+data.get(key)[0]+"',";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql2=sql2.substring(0,sql2.lastIndexOf(","));
        sql+=sql2+")";
        System.out.println(sql);
        Connection connection= MySql.getConnection();
        Statement statement=connection.createStatement();
        if(statement.executeUpdate(sql)!=0){
            return true;
        }
        return false;
    }

    public ReportPO getReport(String userId,String userReportId) throws SQLException {
        Connection connection= MySql.getConnection();
        String sql="select * from report where user_id=? and user_report_id=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,userId);
        statement.setString(2,userReportId);
        ResultSet resultSet=statement.executeQuery();

        ReportPO report=new ReportPO();
        if(resultSet.next()){
            report.setReportId(Integer.parseInt(resultSet.getString("report_id")));
            report.setUserId(resultSet.getString("user_id"));
            report.setUserReportId(resultSet.getString("user_report_id"));
            report.setReportColumnNumber(Integer.parseInt(resultSet.getString("report_column_number")));
            String t;
            if((t=resultSet.getString("t1"))!=null){
                report.setT1(t);
            }
            if((t=resultSet.getString("t2"))!=null){
                report.setT2(t);
            }
            if((t=resultSet.getString("t3"))!=null){
                report.setT3(t);
            }
            if((t=resultSet.getString("t4"))!=null){
                report.setT4(t);
            }
            if((t=resultSet.getString("t5"))!=null){
                report.setT5(t);
            }
            if((t=resultSet.getString("t6"))!=null){
                report.setT6(t);
            }
            if((t=resultSet.getString("t7"))!=null){
                report.setT7(t);
            }
            if((t=resultSet.getString("t8"))!=null){
                report.setT8(t);
            }
            if((t=resultSet.getString("t9"))!=null){
                report.setT8(t);
            }
            if((t=resultSet.getString("t10"))!=null){
                report.setT10(t);
            }
            if((t=resultSet.getString("t11"))!=null){
                report.setT11(t);
            }
            if((t=resultSet.getString("t12"))!=null) {
                report.setT12(t);
            }
        }
        return report;
    }
}
